/* File:        	MgMImpl.java
* Package:			org.esi.authz.mgm
* Created:      	29-abr-2011
* Last Changed: 	0:08:09
* Author:       	Borja Urkizu borja.urquizu@tecnalia.com
*
*   
* History:
*  Revision 1.0.0.0  
* 
* Tecnalia Research and Innovation
*/
package com.tecnalia.authz.pdp.core;

import java.io.IOException;


import com.tecnalia.authz.comm.channel.MgMDChannelImpl;
import com.tecnalia.authz.common.log.PDPLog;
import com.tecnalia.authz.pdp.config.PDPConfig;
import com.tecnalia.comm.channel.IDChannel;
import com.tecnalia.comm.channel.socket.DSocketChannelImpl;
import com.tecnalia.comm.channel.socket.IDSocketChannel;

// TODO: Auto-generated Javadoc
/**
 * The Class MgMImpl.
 */
public class PDPManager {
    
    /** The logger. */
    public static PDPLog logger = null;  
    
    /** The daemon socketchannel. */
    private IDSocketChannel mgmSocketchannel = null;

	/** The pdpconfig. */
	private PDPConfig pdpconfig;

	/**
	 * Instantiates a new manager impl.
	 */
	public PDPManager(){
	}
	
	

	/**
	 * Start.
	 *
	 * @throws Exception the exception
	 */
	public void start() throws Exception {
  	
        //Init the log 
        logger = new PDPLog (this);

        //Not Complete the status impl for the DAEMON 
    	//DaemonStatus.updateProperty(KEY_RUN_STATUS, STATUS_RUN_RUNING);

    	logger.info("PDP MGM Started"); 	   	
    
        pdpconfig = new PDPConfig();        

        //DaemonStatus.updateProperty(KEY_RUN_STATUS, STATUS_RUN_INITIALIZED);
    	logger.info("PDP MGM Initilizazed");

    	//Start to accept conection throught the socket channel
    	IDChannel mgmChannel = new MgMDChannelImpl();
    	
    	//Create the channel socket
    	mgmSocketchannel = new DSocketChannelImpl(pdpconfig.getMgmHost(), 
    												pdpconfig.getMgmPort());
    	
    	mgmSocketchannel.start(mgmChannel);

    }

	/**
	 * Refresh.
	 *
	 * @throws Exception the exception
	 */
	public void refresh() throws Exception {
    	//Stop the socket channel and the 
    	this.stop();
    	
    	this.start();

    	logger.info("MGM Reloaded");

    	//TODO: Not Complete the status impl for the DAEMON 
    	//DaemonStatus.updateProperty(KEY_RUN_STATUS, STATUS_RUN_RUNING);
    }

	/**
	 * Cleanup.
	 */
	public void cleanup(){

    	logger.info("MGM Shutdown");
        
    	//TODO: Not Complete the status impl for the DAEMON 
        //DaemonStatus.updateProperty(KEY_RUN_STATUS, STATUS_RUN_NOTRUN);

    	try {
			mgmSocketchannel.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
    }

	/* (non-Javadoc)
	 * @see org.esi.authz.pdp.core.IDaemon#stop()
	 */
	/**
	 * Stop.
	 */
	public void stop() {
		// TODO Auto-generated method stub
		
	}


	
}
